class Solution {
public:
    vector<int> findOrder(int n, vector<vector<int>>& pre) {
        vector<int> ret;
        unordered_map<int, vector<int>> hash;
        vector<int> in(n);
        for (auto& e : pre)
        {
            hash[e[1]].push_back(e[0]);
            in[e[0]]++;
        }
        queue<int> q;
        for (int i = 0;i < n;i++)
        {
            if (in[i] == 0)  q.push(i);
        }
        while (q.size())
        {
            int tmp = q.front();q.pop();
            ret.push_back(tmp);
            for (auto e : hash[tmp])
            {
                in[e]--;
                if (in[e] == 0) q.push(e);
            }
        }
        for (auto e : in) if (e) return {};
        return ret;
    }
};